﻿#-------------------------------------------------------------------------------
# Name:       colortext
# Purpose:
#
# Author:     SUPER_紫电  QQ,911344755
#
# Created:    03/10/2022
# Copyright:  (c) SUPER_紫电 2022 All rights reserved.
# Licence:    <Modified BSD License>
#-------------------------------------------------------------------------------

import colorama
from colorama import init, Fore, Back, Style, Cursor
from colorama.win32 import *
from colorama.winterm import *
from ctypes import wintypes

'''
from asciimatics.constants import SINGLE_LINE, DOUBLE_LINE, ASCII_LINE
from asciimatics.effects import Print
from asciimatics.exceptions import ResizeScreenError
from asciimatics.renderers import BarChart, VBarChart, FigletText
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.utilities import BoxTool
'''

STDIN  = STD_INPUT_HANDLE  = -10
STDOUT = STD_OUTPUT_HANDLE = -11
STDERR = STD_ERROR_HANDLE  = -12

COORD = wintypes._COORD
SMALL_RECT = wintypes.SMALL_RECT

_GetStdHandle = colorama.win32._GetStdHandle
_FillConsoleOutputCharacter = colorama.win32._FillConsoleOutputCharacterA

fore_color = {
    'black'  : Fore.BLACK,
    'red'    : Fore.RED,
    'green'  : Fore.GREEN,
    'yellow' : Fore.YELLOW,
    'blue'   : Fore.BLUE,
    'magenta': Fore.MAGENTA,
    'cyan'   : Fore.CYAN,
    'while'  : Fore.WHITE,
    'light_black'  : Fore.LIGHTBLACK_EX,
    'light_red'    : Fore.LIGHTRED_EX,
    'light_green'  : Fore.LIGHTGREEN_EX,
    'light_yellow' : Fore.LIGHTYELLOW_EX,
    'light_blue'   : Fore.LIGHTBLUE_EX,
    'light_magenta': Fore.LIGHTMAGENTA_EX,
    'light_cyan'   : Fore.LIGHTCYAN_EX,
    'light_while'  : Fore.LIGHTWHITE_EX,
}

back_color = {
    'black'  : Back.BLACK,
    'red'    : Back.RED,
    'green'  : Back.GREEN,
    'yellow' : Back.YELLOW,
    'blue'   : Back.BLUE,
    'magenta': Back.MAGENTA,
    'cyan'   : Back.CYAN,
    'while'  : Back.WHITE,
    'light_black'  : Back.LIGHTBLACK_EX,
    'light_red'    : Back.LIGHTRED_EX,
    'light_green'  : Back.LIGHTGREEN_EX,
    'light_yellow' : Back.LIGHTYELLOW_EX,
    'light_blue'   : Back.LIGHTBLUE_EX,
    'light_magenta': Back.LIGHTMAGENTA_EX,
    'light_cyan'   : Back.LIGHTCYAN_EX,
    'light_while'  : Back.LIGHTWHITE_EX,
}

init(autoreset=True)

def get_console_size():
    return GetConsoleScreenBufferInfo().dwSize

def cprint(text='', fore='', back=''):
    """
    color: 'black' 'red' 'green' 'yellow' 'blue' 'magenta' 'cyan' 'while'
    light: 'light_color'
    """
    if fore in fore_color:
        result = fore_color[fore]
    if back in back_color:
        result += back_color[back]
    print(result + text + Fore.RESET + Back.RESET)

def errinfo(text=''):
    print(Fore.LIGHTRED_EX + text + Fore.RESET)
    '''
    screen = Screen.open()
    try:
        screen._change_colours(colour=Screen.COLOUR_RED)
        screen._print_at(text=text, x=0, y=screen._last_start_line)
    finally:
            screen.close()
    '''

def draw_box(rc, single=True):
    single_line = [0xda, 0xbf, 0xc0, 0xd9, 0xc4, 0xb3]
    double_line = [0xc9, 0xbb, 0xc8, 0xbc, 0xcd, 0xba]
    box = ['Ú', '¿', 'À', 'Ù', 'Ä', '³']
    if single:
        box = list(map(chr, single_line))
    else:
        box = list(map(chr, double_line))
    #print(box)
    pos = COORD(rc.Left, rc.Top)
    sdtout = _GetStdHandle(STDOUT)
    """
    FillConsoleOutputCharacter(sdtout, box[0], 1, pos)
    for pos.X in range(rc.Left+1, rc.Right, 1):
        FillConsoleOutputCharacter(sdtout, box[4], 1, pos)
    pos.X = rc.Right
    FillConsoleOutputCharacter(sdtout, box[1], 1, pos)
    for pos.Y in range(rc.Top+1, rc.Bottom, 1):
        pos.X = rc.Left
        FillConsoleOutputCharacter(sdtout, box[5], 1, pos)
        pos.X = rc.Right
        FillConsoleOutputCharacter(sdtout, box[5], 1, pos)
    pos.X = rc.Left
    pos.Y = rc.Bottom
    FillConsoleOutputCharacter(sdtout, box[2], 1, pos)
    for pos.X in range(rc.Left+1, rc.Right, 1):
        FillConsoleOutputCharacter(sdtout, box[4], 1, pos)
    pos.X = rc.Right
    FillConsoleOutputCharacter(sdtout, box[3], 1, pos)
    """